package cn.itlaobing.web.servlet;

import cn.itlaobing.service.TrainService;
import cn.itlaobing.web.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Map;

/**
 * Created by kevin on 2017/11/10.
 */
@WebServlet("/train/*")
public class TrainServlet extends BaseServlet {
    @RequestMapping("/query")
    public void query(HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException{
        request.setCharacterEncoding("utf-8");
        String trainNum=request.getParameter("trainNum");
        String statrtStation=request.getParameter("startStation");
        String endStation=request.getParameter("endStation");
        if (trainNum==null ||"".equals(trainNum)){
            request.setAttribute("MSG","车次为空");
            request.getRequestDispatcher("/query.jsp").forward(request,response);
            return;
        }
        if (statrtStation==null || "".equals(statrtStation)){
            request.setAttribute("MSG","起始站为空");
            request.getRequestDispatcher("/query.jsp").forward(request,response);
            return;
        }
        if (endStation==null || "".equals(endStation)){
            request.setAttribute("MSG","终点站为空");
            request.getRequestDispatcher("/query.jsp").forward(request,response);
            return;
        }
        //输入车次、起始站、终点站都正确
        TrainService trainService=new TrainService();
        try{
            Map<String,Object>map=trainService.findAll(trainNum,statrtStation,endStation);
            //查询成功后放入session
            request.getSession().setAttribute("M",map);
            request.getRequestDispatcher("/WEB-INF/views/show.jsp").forward(request,response);
        }catch (SQLException e){
            e.printStackTrace();
        }


    }
}
